<script>
import {
	getSessionKey,
	autoLogin,
} from '@/request/index.js'
export default {
	globalData: {

	},
	onLaunch: function () {

	},
	onShow: function () {

	},
	onHide: function () {

	},
	methods: {
		getUserId(loading, appUserInfo) {
			return new Promise((resolve, reject) => {
				if (!uni.getStorageSync('userid')) {
					uni.getUserProfile({
						desc: '不超过30个字符',
						success(res) {
							loading(true)
							const {
								nickName,
								avatarUrl,
								gender
							} = res.userInfo;
							console.log("userInfo:", res.userInfo);
							uni.login({
								success(login) {
									getSessionKey({
										code: login.code,
									}).then(getOpenid => {
										console.log({
											nickName,
											avatarUrl,
											gender
										});
										autoLogin({
											openid: getOpenid.data.openid,
											NickName: nickName,
											HeadUrl: avatarUrl,
											sex: gender,
										}).then(res => {
											console.log(res.data.userid);
											uni.setStorageSync('userid', res
												.data.userid);
											appUserInfo();
											loading(false);
											resolve(true)
										})
									})
								}
							})
						},
						fail(err) {
							reject(false)
						},
					})
				} else {
					resolve(true)
				}
			})
		},
		autr(yes = () => { }, no = () => { }, loadingStatus, getAppUserInfo) {
			this.getUserId((loading) => {
				loadingStatus(loading)
			}, () => {
				getAppUserInfo()
			}).then(status => {
				if (status) {
					yes()
				}
			}).catch(error => {
				no(error)
			})
		},
	}
}
</script>

<style lang="scss">
/*每个页面公共css */
@import "uview-ui/index.scss";
</style>
